import {
  commonRoutes
} from '@/router'
import store from '@/store'

// 将菜单信息转成对应的路由信息 动态添加
export default function createRoutes(data) {
  let result = []
  const buttons = {}

  if (store.state.login.userInfo.usrid) {
    data.forEach((element, index) => {
      commonRoutes.forEach((dataItem, indexs) => {
        if (element.path === dataItem.path) {
          dataItem.name = element.name
          result.push({
            ...dataItem
          })
          result[index].children = []
          if (dataItem.children) {
            generateRoutes(element.children, dataItem.children, result, index, buttons)
          }
        }
      })
    })
  }
  // 最后添加404页面 否则会在登陆成功后跳到404页面
  result.push({
    path: '*',
    redirect: '/404',
    hidden: true
  })
  store.dispatch('buttonsAction', buttons)

  return result
}
// 按钮权限 数据 信息
// function generateButton(path, data, buttons) {
//   let mySymbol = Symbol()
//   mySymbol = path
//   buttons[mySymbol] ? null : (buttons[mySymbol] = [...data])
// }
function generateRoutes(asyncRoutes, data, result, indexs, buttons) {
  asyncRoutes.forEach(dataItem => {
    data.forEach(element => {
      if (element.path === dataItem.path) {
        element.name = dataItem.name
        result[indexs].children.push(element)
        if (element.children && !element.children[0].isHidden) {
          generateRoutes(element.children, dataItem.children, result)
        }
      }
    })
  })
  // 获取 按钮权限所需的信息
  // data.forEach(dataItem => {
  //   if (dataItem.children) {
  //     generateButton(dataItem.path, dataItem.children, buttons)
  //   }
  // })
}